package com.dwxt.community.exception;

import com.dwxt.common.base.*;
import com.dwxt.community.aiequipment.util.*;
import org.slf4j.*;
import org.springframework.validation.*;
import org.springframework.web.bind.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/**
 * @program: intelligentcity
 * @description: 自定义异常处理
 * @author
 * @create: 2020/05/14
 */
@ControllerAdvice
public class DefinedExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(DefinedExceptionHandler.class);

    @ExceptionHandler(DefinedException.class)
    @ResponseBody
    public BaseResult handleBindException(Exception e) {
        DefinedException definedException = (DefinedException) e;
        logger.error(definedException.getBaseResult().getMsg());
        return definedException.getBaseResult();
    }

    @ExceptionHandler(AiEquipemntException.class)
    @ResponseBody
    public void handleAiEquipemntException(HttpServletResponse response, Exception e){

        try( ServletOutputStream outputStream = response.getOutputStream()){
            AiEquipemntException definedException = (AiEquipemntException) e;
            logger.error(definedException.getBaseResult().getMsg());
            int[] resInts = (int[])definedException.getBaseResult().getData();
            outputStream.write(DataHandler.toByteArray(resInts));
            outputStream.flush();
        }catch (IOException ioE){
            logger.error(ioE.getMessage());
            ioE.printStackTrace();
        }
    }
}
